package com.hbnu.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class DatabaseDemo9 {

    //事务的原子性，一个事务中的操作要么都成功要么都失败
    //主要思想是将事务的自动提交改为手动提交
    //此文件为自动提交，Demo10为手动提交
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            String url = "jdbc:mysql://localhost:3306/class2211?serverTimezone=UTC&useSSL=false";
            String user = "root";
            String password = "12123";

            Connection connection = DriverManager.getConnection(url,user,password);


            String sql = "update nncc1 set c3=? where c1 = ?";
            PreparedStatement preparedStatement =  connection.prepareStatement(sql);

            preparedStatement.setString(1,"3");             //前面的数字是第几个占位符的意思
            preparedStatement.setString(2,"3");

            int rows = preparedStatement.executeUpdate();

            preparedStatement.setString(1,"3");                          //注意设置的类型
            preparedStatement.setString(2,"9");
            rows += preparedStatement.executeUpdate();



            System.out.println("影响了数据库表"+rows+"条数据");

            preparedStatement.close();
            connection.close();


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
